package helpServlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.helpSession.HelpRequestsessionRemote;

public class RefuseOrAcceptHelpRequest extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public RefuseOrAcceptHelpRequest() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		drawPage(request, response);
	}

	private void drawPage(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String what = (String) request.getParameter("decision");
		int idReq = Integer.parseInt(request.getParameter("idReq"));
		Context jndiCtx;
		try {
			jndiCtx = new InitialContext();
			HelpRequestsessionRemote hrs = (HelpRequestsessionRemote) jndiCtx
					.lookup("Progetto/HelpRequestsession/remote");
			if (what.equals("accept")){
				hrs.decisionOnHelpRequest(idReq, 1);
				request.getSession().setAttribute("done", "accepted");
			}
			if (what.equals("refuse")){
				hrs.decisionOnHelpRequest(idReq, 0);
				request.getSession().setAttribute("done", "refused");
			}
			if (what.equals("remove")){
				hrs.eraseRequest(idReq);
				request.getSession().setAttribute("done", "removed");
			}
		} catch (Exception e) {
			request.getSession().setAttribute("error", "error");
		}
		response.sendRedirect("/ProgettoWeb/showHelps.html");
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		drawPage(request, response);
	}

}
